<?php
namespace app\user\controller;

use app\user\model\User as UserModel;
use hema\wechat\Driver;
use think\facade\View;

/**
 * 用户管理
 */
class User extends Controller
{
    /**
     * 编辑
     */
    public function edit()
    {
        $model = UserModel::get($this->user_id);
        if ($this->request->isGet()) {
            if($model){
               return $this->renderSuccess('', '', compact('model')); 
            }
            return $this->renderError('获取失败');
        }
        // 更新记录
        if ($model->edit($this->postData('data'))) {
            return $this->renderSuccess('操作成功');
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
    }
     /**
     * 手机绑定
     */
    public function phoneBind()
    {
        $model = UserModel::get($this->user_id);
        if ($model->phoneBind($this->postData('data'))) {
            return $this->renderSuccess('操作成功');
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
	}
	
	/**
     * 微信绑定
     */
    public function wechatBind()
    {
        $model = UserModel::get($this->user_id);
        if ($model->wechatBind($this->postData('data'))) {
            return $this->renderSuccess('操作成功');
        }
        $error = $model->getError() ?: '操作失败';
        return $this->renderError($error);
	}
	
    /**
     * 代理登陆
     */
    public function agentLogin()
    {
        $model = UserModel::get($this->user_id);
        if ($model->agentLogin()) {
            return redirect('/agent');
        }
        $error = $model->getError() ?: '创建失败';
        return $this->renderError($error);
    }
    /**
     * 获取关注二维码
     */
    public function qrcodeCreate()
    {
        $wx = new Driver;
        if($ticket = $wx->qrcodeCreate()){
            return $this->renderSuccess('操作成功','',compact('ticket'));
        }
        $error = $wx->getError() ?: '二维码获取失败';
        return $this->renderError($error);
    }
}
